package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.PcrResultMapper;
import com.zhijian.medical.entity.po.PcrResult;
import com.zhijian.medical.entity.vo.request.PcrResultDetailSearchVo;
import com.zhijian.medical.entity.vo.request.PcrResultSearchVo;
import com.zhijian.medical.entity.vo.response.PcrClassifyResultVo;
import com.zhijian.medical.entity.vo.response.PcrResultVo;
import com.zhijian.medical.enums.KnbEnum;
import com.zhijian.medical.service.PcrResultDetailService;
import com.zhijian.medical.service.PcrResultService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * PCR-结果业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-08-30 15:35:22
 */
@Slf4j
@Service
public class PcrResultServiceImpl implements PcrResultService {

    @Autowired
    private PcrResultMapper mapper;

    @Autowired
    private PcrResultDetailService pcrResultDetailService;

    @Override
    public PcrResult selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PcrResultVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByPcrId(Long pcrId) {
        mapper.deleteByPcrId(pcrId);
    }

    @Override
    public void deleteByPcrIdAndDeviceId(Long pcrId, Long deviceId) {
        mapper.deleteByPcrIdAndDeviceId(pcrId, deviceId);
    }

    @Override
    public void deleteByPcrKzyId(Long pcrKzyId) {
        mapper.deleteByPcrKzyId(pcrKzyId);
    }

    @Override
    public Long insert(PcrResult pcrResult) {
        Date now = new Date();
        pcrResult.setCreateTime(now);
        pcrResult.setUpdateTime(now);
        return mapper.insert(pcrResult) > 0 ? pcrResult.getId() : 0L;
    }

    @Override
    public boolean update(PcrResult pcrResult) {
        Date now = new Date();
        pcrResult.setUpdateTime(now);
        return mapper.updateByPrimaryKey(pcrResult) != 0;
    }

    @Override
    public Integer count(PcrResultSearchVo pcrResultSearchVo) {
        return mapper.count(pcrResultSearchVo);
    }

    @Override
    public List<PcrResultVo> selectList(PcrResultSearchVo pcrResultSearchVo) {
        List<PcrResultVo> pcrResultVos = mapper.selectList(pcrResultSearchVo);
        if (CollectionUtil.isEmpty(pcrResultVos)) {
            return Collections.emptyList();
        }
        pcrResultVos.forEach(p -> p.setPcrResultDetailList(pcrResultDetailService.selectList(PcrResultDetailSearchVo.builder()
                .resultId(p.getId())
                .build())));
        return pcrResultVos;
    }

    @Override
    public List<PcrClassifyResultVo> selectClassifyList(PcrResultSearchVo pcrResultSearchVo) {
        List<PcrResultVo> pcrResultVos = mapper.selectList(pcrResultSearchVo);
        if (CollectionUtil.isEmpty(pcrResultVos)) {
            return Collections.emptyList();
        }
        //去重后的病理号
        List<String> blhList = pcrResultVos.stream().map(PcrResultVo::getBlh).distinct().collect(Collectors.toList());
        return blhList.stream().map(blh -> {
            PcrClassifyResultVo pcrClassifyResultVo = new PcrClassifyResultVo();
            pcrClassifyResultVo.setBlh(blh);
            for (PcrResultVo pcrResultVo : pcrResultVos) {
                if (pcrResultVo.getBlh().equals(blh)) {
                    if (pcrResultVo.getProject().equals(KnbEnum.KRAS.name())) {
                        pcrClassifyResultVo.setKrasPcrResultDetailList(pcrResultDetailService.selectList(PcrResultDetailSearchVo.builder()
                                .resultId(pcrResultVo.getId())
                                .build()));
                    } else if (pcrResultVo.getProject().equals(KnbEnum.NRAS.name())) {
                        pcrClassifyResultVo.setNrasPcrResultDetailList(pcrResultDetailService.selectList(PcrResultDetailSearchVo.builder()
                                .resultId(pcrResultVo.getId())
                                .build()));
                    } else {
                        pcrClassifyResultVo.setBrafPcrResultDetailList(pcrResultDetailService.selectList(PcrResultDetailSearchVo.builder()
                                .resultId(pcrResultVo.getId())
                                .build()));
                    }
                }
            }
            return pcrClassifyResultVo;
        }).collect(Collectors.toList());
    }

    @Override
    public Page<PcrResultVo> selectPage(PcrResultSearchVo pcrResultSearchVo) {
        //先直接查总数
        int count = count(pcrResultSearchVo);
        //分页
        Page<PcrResultVo> page = new Page<>(true, count, pcrResultSearchVo.getPageNum(), pcrResultSearchVo.getPageSize());
        pcrResultSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PcrResultVo> responseVos = this.selectList(pcrResultSearchVo);
        page.setData(responseVos);
        return page;
    }

}